<script setup>
const { proxy } = getCurrentInstance();
import { GetDeptPI } from "@/api/common/index";

/**
 * 传入数据
 */
const props = defineProps({
  data: {
    type: Object,
    default: () => {},
  },
  title: {
    type: String,
    default: "",
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: "",
  },
});

/**
 * 表单实例
 */
const ruleFormRef = ref(null);

/**
 *部门
 */
const deptData = ref([]);

/**
  稽查类别
   */
const list = () => {
  if (props.type == "固定式钢构") {
    switch ("fixed_inspection_category" + ruleForm.inspectionScene) {
      case "fixed_inspection_category1":
        return fixed_inspection_category1.value;

      case "fixed_inspection_category2":
        return fixed_inspection_category2.value;
      case "fixed_inspection_category3":
        return fixed_inspection_category3.value;

      default:
        return [];
    }
  } else if (props.type == "厂务") {
    switch ("facility_inspection_category" + ruleForm.inspectionScene) {
      case "facility_inspection_category1":
        return facility_inspection_category1.value;

      case "facility_inspection_category2":
        return facility_inspection_category2.value;
      case "facility_inspection_category3":
        return facility_inspection_category3.value;
      case "facility_inspection_category4":
        return facility_inspection_category4.value;
      default:
        return [];
    }
  } else {
    return [];
  }
};

/**
 * 树结构枚举
 */
const defaultProps = {
  children: "children",
  label: "categoryName",
  value: "id",
};
// 表单字段
const ruleForm = reactive({
  type: props.type == "固定式钢构" ? 1 : 2,
  status: "",

  remark: "",
});

/**
 * 表单校验
 */
const rules = reactive({
  inspectionScene: [{ required: true, message: "稽查场景", trigger: "change" }],
  auditType: [{ required: true, message: "稽查类别", trigger: "change" }],
});

/**稽查场景 */
const inspectionChange = (v) => {};

/**
 * 提交方法
 */
const submit = async () => {
  const valid = await ruleFormRef.value.validate();
  if (!valid) return false;
  if (
    props.type == "厂务" &&
    ruleForm.inspectionScene == "厂务供电" &&
    !ruleForm.grade
  ) {
    proxy.warningMsg("请先填写 等级！");
    return false;
  }

  return ruleForm;
};

/**
 * 初始化数据
 */
const initData = () => {
  if (props.data?.id) {
    Object.keys(props.data).forEach(
      (itemKey) => (ruleForm[itemKey] = props.data[itemKey])
    );
  }
};

/**
 * 回显数据赋值
 */
initData();
defineExpose({ submit });
</script>

<template>
  <div class="app-container">
    <!-- 新增 表单 -->
    <el-form
      :model="ruleForm"
      label-width="120px"
      :rules="rules"
      status-icon
      inline
      ref="ruleFormRef"
    >
      <el-form-item label="稽查场景" prop="inspectionScene">
        <el-input
          :disabled="props.disabled"
          v-model="ruleForm.inspectionScene"
          placeholder="请输入 稽查场景"
        ></el-input>
      </el-form-item>

      <el-form-item label="稽查类别" prop="auditType">
        <el-input
          :disabled="props.disabled"
          v-model="ruleForm.auditType"
          placeholder="请输入 稽查类别"
        ></el-input>
      </el-form-item>
      <el-form-item label="等级" v-if="props.type == '厂务'">
        <el-input
          :disabled="props.disabled"
          v-model="ruleForm.grade"
          placeholder="请输入 等级"
        ></el-input>
      </el-form-item>
      <div class="rk">
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            type="textarea"
            clearable
            v-model="ruleForm.remark"
            :disabled="props.disabled"
          />
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<style lang="scss" scoped>
.rk ::v-deep .el-form-item {
  width: 100% !important;
}
</style>